package com.qyhl.webtv.module_microvideo.shortvideo.theme;

import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.qyhl.webtv.basiclib.base.BaseFragment;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.microvideo.ShortVideoThemeBean;
import com.qyhl.webtv.commonlib.utils.ResizableImageView;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.qyhl.webtv.module_microvideo.R;
import com.qyhl.webtv.module_microvideo.R2;
import com.qyhl.webtv.module_microvideo.shortvideo.ShortVideoActivity;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 主题列表
 */
public class ShortVideoThemeFragment extends BaseFragment implements ShortVideoThemeContract.ShortVideoThemeView {
    @BindView(R2.id.recycle_view)
    RecyclerView recycleView;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;

    private String activityId;

    private CommonAdapter commonAdapter;

    private ShortVideoThemePresenter mPresenter;

    private List<ShortVideoThemeBean> mDataList = new ArrayList<>();

    public void setActivityId(String activityId) {
        this.activityId = activityId;
    }


    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.micro_fragment_short_video_theme, null);
    }

    @Override
    protected void create() {
        mPresenter = new ShortVideoThemePresenter(this);
        initView();

    }

    @Override
    protected void setListener() {
        setListeners();
        mPresenter.getList(activityId);
    }

    @Override
    protected void onFirstVisible() {

    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void onHide() {

    }

    public static ShortVideoThemeFragment newInstance(String actId) {
        ShortVideoThemeFragment fragment = new ShortVideoThemeFragment();
        fragment.setActivityId(actId);
        return fragment;
    }


    private void initView() {
        loadMask.setStatus(LoadingLayout.Loading);
        refresh.setRefreshHeader(new MaterialHeader(getContext()));
        refresh.setEnableLoadMore(false);

        //设置布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(linearLayoutManager);

        recycleView.setAdapter(commonAdapter = new CommonAdapter<ShortVideoThemeBean>(getContext(), R.layout.micro_item_short_video_theme, mDataList) {
            @Override
            protected void convert(ViewHolder holder, ShortVideoThemeBean bean, int position) {
                ResizableImageView cover = holder.getView(R.id.cover);
                Glide.with(getContext())
                        .load(bean.getLogo())
                        .apply(new RequestOptions().placeholder(R.drawable.cover_large_default).error(R.drawable.cover_large_default))
                        .into(cover);
                holder.setText(R.id.title, "#" + bean.getName() + "#");
                holder.setText(R.id.summary, bean.getInformation());
            }
        });

    }

    private void setListeners() {
        loadMask.setOnReloadListener(v -> {
            loadMask.setReloadButtonText("加载中...");
            mPresenter.getList(activityId);
        });

        refresh.setOnRefreshListener(refreshLayout -> mPresenter.getList(activityId));

        commonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {

                Bundle bundle = new Bundle();
                bundle.putString("id", mDataList.get(position).getId() + "");
                bundle.putString("actId", activityId);
                RouterManager.router(ARouterPathConstant.SHORT_VIDEO_THEME_DETAIL,bundle);

            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });

        recycleView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                ((ShortVideoActivity) getActivity()).showHideBottombar(dy);
            }
        });
    }

    @Override
    public void setList(List<ShortVideoThemeBean> beanList) {
        refresh.finishRefresh();
        loadMask.setStatus(LoadingLayout.Success);
        loadMask.setReloadButtonText("点击重试~");

        mDataList.clear();
        mDataList.addAll(beanList);
        commonAdapter.notifyDataSetChanged();
    }

    @Override
    public void setError(String msg) {
        refresh.finishRefresh();
        loadMask.setReloadButtonText("点击重试~");
        loadMask.setStatus(LoadingLayout.Error);
        loadMask.setErrorText(msg);
        if (msg.contains("网络")) {
            loadMask.setErrorImage(R.drawable.error_network);
        } else if (msg.contains("暂无")) {
            loadMask.setErrorImage(R.drawable.empty_content);
        } else {
            loadMask.setErrorImage(R.drawable.error_content);
        }
    }

}
